/*
 * AbstractDepartmentServlet.java
 * Created on 25. juni 2008, 10:52
 */
package uia.is202.jpademo.web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * AbstractDepartmentServlet is the abstract superclass of
 * all servlets in the application. It serves a single
 * home to common methods.
 * 
 * @author even
 */
public abstract class AbstractDepartmentServlet extends HttpServlet {
    @PersistenceUnit
    protected EntityManagerFactory emf;
    
    
    /**
     * Handle get requests by calling processRequest()
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
        processRequest(req, resp);
    }
    
    
    /**
     * Handle post requests by calling processRequest()
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
        processRequest(req, resp);
    }
    
    
    /**
     * Handle requests. Sends a html page containing the department list.
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException ;
    
    
    // Utility methods to help printing html pages
    protected void printField(PrintWriter out, String label, String name, String value) {
        out.print("<tr><td>");
        out.print(label);
        out.print(":</td><td><input type=\"text\" name=\"");
        out.print(name);
        out.print("\" value=\"");
        out.print(value);
        out.println("\"/></td></tr>");
    }

    protected void printField(PrintWriter out, String label, String name, Long value) {
        printField(out, label, name, value.toString());
    }
    
    protected void printDatum(PrintWriter out, String label, String value) {
        out.print("<tr><td>");
        out.print(label);
        out.print(":</td><td>");
        out.print(value);
        out.println("</td></tr>");
    }

    protected void printDatum(PrintWriter out, String label, Long value) {
        printDatum(out, label, value.toString());
    }
}
